<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>

    <script type="text/html" id="currentTableBar">
        {{#  if(d.ifAvailableName == "启用"){ }}
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="disable">禁用</button>
        {{#  } else { }}
        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="enable">启用</button>
        {{#  } }}
        <button class="layui-btn layui-btn-sm" lay-event="updateUser">修改</button>
    </script>

    <script type="text/html" id="adminTableBar">
        <a class="layui-btn layui-btn-xs" lay-event="disable">/user/upd/admin</a>
    </script>


</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            roleType = layui.data("user").roleType,
            table = layui.table,
            basecols = [[
                // {type: "checkbox", width: 50},
                // {field: 'id', width: 80, title: 'ID'},
                {field: 'ifAvailableName', title: '启用状态'},
                {field: 'nickname', title: '昵称'},
                {field: 'username', title: '帐号'},
                // {field: 'parentId', title: 'parentId'},
                {field: 'roleTypeName', title: '角色类型'},
                {field: 'statusName', title: '员工状态'}
            ]];

        if (roleType === "SUPERVISOR") {
            basecols[0].push({title: '操作', toolbar: '#currentTableBar', align: "center"});
        } else if (roleType === "ADMIN") {
            // 这个接口预留  先不写
            // basecols[0].push({title: '操作',toolbar: '#adminTableBar', align: "center"});
        }

        table.render({
            elem: '#currentTableId',
            // url: '../api/user.json',
            method: 'post',
            url: '/zxjy/user/list/' + roleType.toLowerCase(),
            toolbar: '#toolbarDemo',
            contentType: 'application/json',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cellMinWidth: 100,
            cols: basecols,
            response: {statusCode: 200},
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.state, //解析接口状态
                    "msg": res.success, //解析提示文本
                    "count": res.result.total, //解析数据长度
                    "data": res.result.list //解析数据列表
                };
            },
            request: {
                pageName: 'pageNum' // 页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });

            //执行搜索重载
            table.reload('currentTableId', {
                url: '/zxjy/user/list/' + roleType.toLowerCase()
                , where: {
                    wechatNo: data.field.wechatNo,
                    phone: data.field.phone,
                    nickname: data.field.nickname,
                    highSeasStatus: data.field.highSeasStatus,
                } //设定异步数据接口的额外参数
            });


            //执行搜索重载
            // table.reload('currentTableId', {
            //     page: {
            //         curr: 1
            //     }
            //     , where: {
            //         wechatNo: result.wechatNo
            //     }
            // }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加',
                    type: 2,
                    // shade: 0.2,
                    // maxmin:true,
                    shadeClose: true,
                    offset: '150px',
                    area: ['500px', '300px'],
                    content: '../page/table/adduser.html',
                    end: function () {
                        //执行搜索重载
                        table.reload('currentTableId', {
                            url: '/zxjy/user/list/' + roleType.toLowerCase()
                            , where: {
                                wechatNo: data.field.wechatNo,
                                phone: data.field.phone,
                                nickname: data.field.nickname,
                                highSeasStatus: data.field.highSeasStatus,
                            } //设定异步数据接口的额外参数
                        });
                    }
                });
                // $(window).on("resize", function () {
                //     layer.full(index);
                // });
            }
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var param = {}
            switch (obj.event) {
                case "enable":
                    param["userId"] = data.id;
                    param["userAvailableStatus"] = "AVAILABLE";
                    layer.confirm('是否确认启用?', function (index) {
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "/zxjy/user/upd/" + roleType.toLowerCase(),
                            data: JSON.stringify(param),
                            headers: {
                                'Authorization': layui.data("Authorization")['Authorization'],
                            },
                            success: function (result) {
                                if (result.state === 200) {
                                    obj.del();
                                    layer.alert("启用成功")
                                    table.reload("currentTableId");
                                }
                            }
                        });
                    });
                    break;
                case "disable":
                    param["userId"] = data.id;
                    param["userAvailableStatus"] = "NOT_AVAILABLE";
                    layer.confirm('是否确认禁用?', function (index) {
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "/zxjy/user/upd/" + roleType.toLowerCase(),
                            data: JSON.stringify(param),
                            headers: {
                                'Authorization': layui.data("Authorization")['Authorization'],
                            },
                            success: function (result) {
                                if (result.state === 200) {
                                    obj.del();
                                    layer.alert("禁用成功")
                                    table.reload("currentTableId");
                                }
                            }
                        });
                    });
                    break;
                case "updateUser":
                    var index = layer.open({
                        title: '修改用户',
                        type: 2,
                        shadeClose: true,
                        offset: 'auto',
                        area: ['500px','400px'],
                        content: '../page/table/edituser.html',
                        success: function(layero, index) {
                            $("#id", layero.find("iframe")[0].contentWindow.document).attr("value",  data.id);
                            $("#nickname", layero.find("iframe")[0].contentWindow.document).attr("value",  data.nickname);
                            $("#username", layero.find("iframe")[0].contentWindow.document).attr("value",  data.username);
                            $("#statusName", layero.find("iframe")[0].contentWindow.document).attr("value",  data.statusName);
                        },
                        end: function () {
                            //执行搜索重载
                            table.reload('currentTableId', {
                                url: '/zxjy/user/list/' + roleType.toLowerCase()
                                , where: {
                                    wechatNo: data.field.wechatNo,
                                    phone: data.field.phone,
                                    nickname: data.field.nickname,
                                    highSeasStatus: data.field.highSeasStatus,
                                } //设定异步数据接口的额外参数
                            });
                        }
                    });
                    break;
            }
        });

    });
</script>

</body>
</html>
